<?php
    class MenuAction extends AdminAction{
        
		//栏目管理列表
		Public function index(){
			$menu=new MenuModel('Menu');
			$list=$menu->menulist(null);
			$this->assign('list',$list);
			$this->display();
		}
		
		//添加栏目
		Public function add(){
			$menu=new MenuModel('Menu');
			//栏目列表
			$list=$menu->menulist();
			$select=array();
			for ($i=0,$j=count($list);$i<$j;$i++){
				$select[$list[$i]['typeid']]=$list[$i]['name'];
			}	
			$this->assign('select',$select);
			//内容模型列表
			$modelList=$menu->model();
			$this->assign('catlist',$modelList);
			//radios单选选项
			$xx=array(1=>是,0=>否);	//单选参数
			$this->assign('xx',$xx);
			//栏目ID
			if($_GET['id']){$this->assign('typeid',$_GET['id']);}
			//编辑器
			$this->assign('ck',Edit::editor('content','full'));	
			$this->display();
		}
		
		//添加栏目流程
		Public function adding(){
			$menu=D('Menu');
			
			$_POST['reveal']=$_POST['reveal'][0];
			$_POST['root']=$_POST['root'][0];
			$_POST['page']=$_POST['page'][0];
			$_POST['list']=$_POST['list'][0];
			$_POST['domain']=$_POST['domain'][0];
			$_POST['num']=999;
			$_POST['files']=self::pic_uploads($_POST);
			unset($_POST['type']);
			$data=$_POST;

			if($menu->create()){
				$data=$_POST;
				$res=$menu->relation(true)->add($data);
				if($res){
					$this->success('栏目添加成功！','index');
				}else{
					return;
					$this->error('栏目添加失败！');
				}
			}else{
				$this->error($menu->getError());
			}
		
		}
		
		//栏目编辑页
		Public function edit(){
			$menu=new MenuModel('Menu');
			$modelList=$menu->model();
			$this->assign('catlist',$modelList);

			//radio单选参数
			$xx=array(1=>'是',0=>'否');	$this->assign('xx',$xx);
			
			//获取数据
			$menu=M('Menu');
			$res=$menu->where(array('typeid'=>$_GET['id']))->find();
			$res['content']=stripcslashes($res['content']);

			$this->assign('catid',$res['catid']);		//内容模型ID
			$this->assign('page',$res['page']);			//是否生成内容页
			$this->assign('list',$res['list']);			//是否生成栏目页
			$this->assign('domain',$res['domain']);		//是否开启二级域名
			$this->assign('reveal',$res['reveal']);		//是否导航显示
			$this->assign('root',$res['root']);			//是否生成根目录 
			$this->assign('type',$res['type']);			//封面设计
			$this->assign('res',$res);
			$this->assign('typeid',array(0=>$res['typeid']));

			//输出栏目列表
			$select=$this->typeselect(null);
			$this->assign('select',$select);
			$this->assign('pid',$res['pid']);			//上级栏目ID

			//如果该栏目下有内容则禁止修改内容模型和上级栏目
			if($result){
				//查询栏目下是否存在数据
				$sql=M('Article');
				$res=$sql->where(array('typeid'=>$res['typeid']))->find();
				//查询栏目下是否存在子栏目
				$son=$menu->where(array('pid'=>$_GET['id']))->find();

				//如果有内容禁止修改内容模型
				if($res || $son){
					$this->assign('dis',true);	//有内容则止禁修改内容模型
				}else{
					$this->assign('dis',null);
				}		
			}


			$this->assign('ck',Edit::editor('content','full'));
			//输出模板
			$this->display();
		}
		
		//栏目更新
		Public function update(){

			$menu=M('Menu');
			
			$_POST['reveal']=$_POST['reveal'][0];
			$_POST['root']=$_POST['root'][0];
			$_POST['page']=$_POST['page'][0];
			$_POST['list']=$_POST['list'][0];
			$_POST['domain']=$_POST['domain'][0];
			$data=$_POST;

			$where['typeid']=$data['typeid'];
			$res=$menu->where($where)->save($data);

			if($res!==false){
				$files=self::pic_uploads($_POST);
				$db_files=M('Files');
				foreach($files as $k => $v){
					$result=$db_files->where($v)->find();
					if(!$result){
						$v['tid']=$data['typeid'];
						$db_files->add($v);
					}
				}
				$this->success('修改成功','',999);
			}else{
				$this->error('修改失败','',999);
			}


		}
		
		//删除栏目
		Public function del(){
			$menu=M('Menu');

			//检查栏目下是否有内容
			$id=$menu->where(array('typeid'=>$_GET['id']))->field('catid')->find();	

			//获得栏目模型ID
			$res=M('Article')->where(array('typeid'=>$_GET['id']))->field('typeid')->select();

			//检查是否存在子栏目
			$pids=$menu->where(array('pid'=>$_GET['id']))->field('pid')->select();

			if($pids || $res){
				$this->error('操作失败：您要删除的栏目下存在子栏目或内容！');
			}else{
				$res=$menu->where(array('typeid'=>$_GET['id']))->delete();
				if($res){
					$this->success('栏目删除成功！');
				}else{
					$this->error('栏目删除失败！');
				}
			}

		}

		public function num(){	

			$arr=$this->_post();

			$menu=M('Menu');
			foreach ($arr as $key => $value) {
				$where['typeid']=ltrim($key,'num');
				if (!is_numeric($value)) {
					$this->error('排序值不正确！');
				}
				$data['num']=($value==0)?999:$value;
				$res=$menu->where($where)->save($data);
				
				if($res===false){
					$this->error('更新出错！');
				}
			}

			$this->success('更新成功！');		

		}
	
    }
